package com.yj.springbootstudy.mulThread.juctools;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <h1>CountDownLatch：允许一个或者多个线程等待其他线程完成操作。</h1>
 */
public class CountDownLatchDemo {

//    static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor()
    static ExecutorService executorService = Executors.newFixedThreadPool(2);
    static CountDownLatch latch = new CountDownLatch(5);


    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i % 2 == 0) {
                    System.out.println(i);
                }
                latch.countDown();
            }
        },"t1");

        t1.start();
        latch.await();
        System.out.println("main end");


    }
}
